%eyelink2_behavioral_anaysis

clc
clear
close all
warning off MATLAB:divideByZero
scnsize = get(0,'ScreenSize');
graph_suru=0;
all_file_names=[''];
eyelink.file_name=[];
eyelink.subject=[];
eyelink.filenum=[];  
eyelink.subjectnum=[];
eyelink.quad=[];
eyelink.anti=[];
eyelink.peri=[];
eyelink.srt=[];
eyelink.all_srr=[];
eyelink.cor_srr=[];
eyelink.error=[];
eyelink.delay=[];
eyelink.long=[];
eyelink.sac_thres=[];
srt_pro=[];
srt_anti=[];
srt_error=[];
srt_pro_peri=[];
srt_anti_peri=[];
srt_error_peri=[];
srt_pro_cent=[];
srt_anti_cent=[];
srt_error_cent=[];
srt_pro_pro  =[];
srt_anti_anti=[];
srt_anti_pro =[];
srt_pro_anti=[];

cd 'C:\_bcoe\data_eyelink2\_C_DATA'
% folders = dir;
% fred=[];
% for i=1:length(folders)
%     fred(i)=isdir(folders(i).name) & length(folders(i).name)>2; 
% end
% folders(~fred)=[];
% clear fred;index=1;
files=dir('*c.mat');
for i=1:length(files)
    files(i).name
    load(files(i).name);
    C.srt=round(C.srt);
    if isfield(C,'error')==0
        disp([' ''error'' field is not initialized in struct C for file ' files(i).name])
        eyelink2_quad_plot03(files(i).name)
        load(files(i).name);
    end
    if eyelink.filenum
        eyelink.filenum   =[eyelink.filenum;   (eyelink.filenum(end)+1)*ones(size(C.anti))];
    else
        eyelink.filenum   =[eyelink.filenum;   1*ones(size(C.anti))];
    end
    if i>1
        if eyelink.file_name(end,1)~=C.name(1) | eyelink.file_name(end,2)~=C.name(2)
            sub_num=sub_num+1;
            eyelink.subject=[eyelink.subject; C.name(1:2)];
        end            
    else
        sub_num=1;
        eyelink.subject=C.name(1:2);
    end
    eyelink.file_name=[eyelink.file_name; C.name];    
    eyelink.subjectnum=[eyelink.subjectnum;sub_num*ones(size(C.anti))];
    eyelink.anti     =[eyelink.anti;      C.anti];
    eyelink.peri     =[eyelink.peri;      C.peri];
    eyelink.srt      =[eyelink.srt;       round(C.srt)'];
    eyelink.error    =[eyelink.error;     C.error'];
    eyelink.delay    =[eyelink.delay;     C.delay];
    eyelink.long     =[eyelink.long;      diff(C.timeb(:,1))>10000];
    eyelink.sac_thres=[eyelink.sac_thres; C.sac_thres'];
    
    eyelink.quad=[ eyelink.quad; C.quad'];
    fred=(tiedrank(C.srt(C.error==0 | C.error==3))/(sum(C.error==0 | C.error==3)+1))*100;
    rank_srt=C.quad*0;
%    rank_srt(C.srt>0,:)=(fred(:,3)/max(fred(:,3))*100);
    rank_srt((C.error==0 | C.error==3))=fred;
    eyelink.all_srr=[eyelink.all_srr; rank_srt'];
    
    %fred=group_rank(C.srt(C.error==0))';
    fred=(tiedrank(C.srt(C.error==0))/(sum(C.error==0)+1))*100;
    rank_srt=C.quad*0;
%    rank_srt(C.srt>0,:)=(fred(:,3)/max(fred(:,3))*100);
    rank_srt(C.error==0)=fred;
    eyelink.cor_srr=[eyelink.cor_srr; rank_srt'];

    status(i/length(files));
    clc;
end%    for i=1:length(files)
status(-1)

save('eyelink_all','eyelink');

eyelink2.file_name =eyelink.file_name(unique(eyelink.filenum(eyelink.long==0)),:);
eyelink2.filenum   =eyelink.filenum(eyelink.long==0);    
eyelink2.subjectnum=eyelink.subjectnum(eyelink.long==0);  
eyelink2.subject   =eyelink.subject(unique(eyelink2.subjectnum),:);
eyelink2.quad      =eyelink.quad(eyelink.long==0);    
eyelink2.anti      =eyelink.anti(eyelink.long==0);    
eyelink2.peri      =eyelink.peri(eyelink.long==0);    
eyelink2.srt       =eyelink.srt(eyelink.long==0);    
eyelink2.all_srr   =eyelink.all_srr(eyelink.long==0);    
eyelink2.cor_srr   =eyelink.cor_srr(eyelink.long==0);    
eyelink2.error     =eyelink.error(eyelink.long==0);    
eyelink2.delay     =eyelink.delay(eyelink.long==0);    
eyelink2.long      =eyelink.long(eyelink.long==0);    
eyelink2.sac_thres =eyelink.sac_thres(eyelink.long==0);   
temp=unique(eyelink2.filenum);
for i=1:length(temp);
    eyelink2.filenum(eyelink2.filenum==temp(i))=i;
end
temp=unique(eyelink2.subjectnum);
for i=1:length(temp);
    eyelink2.subjectnum(eyelink2.subjectnum==temp(i))=i;
end
eyelink2

eyelink.file_name(unique(eyelink.filenum(eyelink.long==0)),:)=[];
eyelink.filenum(eyelink.long==0)=[]; 
eyelink.subjectnum(eyelink.long==0)=[];  
eyelink.quad(eyelink.long==0)=[];
eyelink.anti(eyelink.long==0)=[];  
eyelink.peri(eyelink.long==0)=[]; 
eyelink.srt(eyelink.long==0)=[];
eyelink.all_srr(eyelink.long==0)=[];    
eyelink.cor_srr(eyelink.long==0)=[];    
eyelink.error(eyelink.long==0)=[];   
eyelink.delay(eyelink.long==0)=[]; 
eyelink.sac_thres(eyelink.long==0)=[];
temp=unique(eyelink.filenum);
for i=1:length(temp);
    eyelink.filenum(eyelink.filenum==temp(i))=i;
end

eyelink.long(eyelink.long==0)=[];
eyelink
save('eyelink_s','eyelink2');
save('eyelink_l','eyelink');
return
% clear; load('eyelink'); scnsize = get(0,'ScreenSize');

    
%this shows that the cut off for early srt should be 300ms
s_p_pro_o=[];
s_p_pro_x=[];
s_c_pro_o=[];
s_c_pro_x=[];
s_p_anti_o=[];
s_p_anti_x=[];
s_c_anti_o=[];
s_c_anti_x=[];
errors=floor(eyelink.error);
bin=10;
for i=10:bin:1500
    s_p_pro_o (end+1)=sum(eyelink.srt>=i & eyelink.srt<i+bin & errors< 3 & eyelink.anti==0 & eyelink.peri==1);
    s_p_pro_x (end+1)=sum(eyelink.srt>=i & eyelink.srt<i+bin & errors==3 & eyelink.anti==0 & eyelink.peri==1);
    s_p_anti_o(end+1)=sum(eyelink.srt>=i & eyelink.srt<i+bin & errors< 3 & eyelink.anti==1 & eyelink.peri==1);
    s_p_anti_x(end+1)=sum(eyelink.srt>=i & eyelink.srt<i+bin & errors==3 & eyelink.anti==1 & eyelink.peri==1);
    s_c_pro_o (end+1)=sum(eyelink.srt>=i & eyelink.srt<i+bin & errors< 3 & eyelink.anti==0 & eyelink.peri==0);
    s_c_pro_x (end+1)=sum(eyelink.srt>=i & eyelink.srt<i+bin & errors==3 & eyelink.anti==0 & eyelink.peri==0);
    s_c_anti_o(end+1)=sum(eyelink.srt>=i & eyelink.srt<i+bin & errors< 3 & eyelink.anti==1 & eyelink.peri==0);
    s_c_anti_x(end+1)=sum(eyelink.srt>=i & eyelink.srt<i+bin & errors==3 & eyelink.anti==1 & eyelink.peri==0);
end
p_max_o=max([s_p_pro_o,s_p_anti_o]),
c_max_o=max([s_c_pro_o,s_c_anti_o]),

p_pro_x_rate=s_p_pro_x./(s_p_pro_o+s_p_pro_x);
p_pro_x_rate(isnan(p_pro_x_rate))=.5;
c_pro_x_rate=s_c_pro_x./(s_c_pro_o+s_c_pro_x);
c_pro_x_rate(isnan(c_pro_x_rate))=.5;

p_anti_x_rate=s_p_anti_x./(s_p_anti_o+s_p_anti_x);
p_anti_x_rate(isnan(p_anti_x_rate))=.5;
c_anti_x_rate=s_c_anti_x./(s_c_anti_o+s_c_anti_x);
c_anti_x_rate(isnan(c_anti_x_rate))=.5;

all_x_rate=(s_c_anti_x+s_p_anti_x+s_c_pro_x+s_p_pro_x)./(s_p_anti_o+s_p_anti_x+s_c_anti_o+s_c_anti_x+s_c_pro_o+s_c_pro_x+s_p_pro_o+s_p_pro_x);
all_x_rate(isnan(all_x_rate))=.5;

fig(201),clf;hold on;
subplot(5,1,1)
hplot(10:bin:1500,p_pro_x_rate,'k')
hplot(10:bin:1500,s_p_pro_o/p_max_o,'b');
hplot(10:bin:1500,-s_p_pro_x/p_max_o,'b');
hline(0);
vline(290,'g');vline(1200,'g');
subplot(5,1,2)
hplot(10:bin:1500,c_pro_x_rate,'k')
hplot(10:bin:1500,s_c_pro_o/c_max_o,'b');
hplot(10:bin:1500,-s_c_pro_x/c_max_o,'b');
hline(0);
vline(290,'g');vline(1200,'g');
subplot(5,1,3)
hplot(10:bin:1500,p_anti_x_rate,'k')
hplot(10:bin:1500,s_p_anti_o/p_max_o,'r');
hplot(10:bin:1500,-s_p_anti_x/p_max_o,'r');
hline(0);
vline(290,'g');vline(1200,'g');
subplot(5,1,4);cla
hplot(10:bin:1500,c_anti_x_rate,'k')
hplot(10:bin:1500,s_c_anti_o/c_max_o,'r');
hplot(10:bin:1500,-s_c_anti_x/c_max_o,'r');
hline(0);
vline(290,'g');vline(1200,'g');
subplot(5,1,5);cla
hplot(10:bin:1500,all_x_rate,'k')
hplot(10:bin:1500,(s_p_anti_o+s_c_anti_o+s_c_pro_o+s_p_pro_o)/max(s_p_anti_o+s_c_anti_o+s_c_pro_o+s_p_pro_o),'m');
hplot(10:bin:1500,-(s_p_anti_x+s_c_anti_x+s_c_pro_x+s_p_pro_x)/max(s_p_anti_o+s_c_anti_o+s_c_pro_o+s_p_pro_o),'m');
hline(0);
vline(290,'g');vline(1200,'g');




axis tight;
%this sets the cut off for early srt at 300ms
eyelink.error(eyelink.srt< 290)=eyelink.error(eyelink.srt< 290)+.4;
%this sets the cut off for late  srt at 1000ms
eyelink.error(eyelink.srt>1200)=eyelink.error(eyelink.srt>1200)+.4;

save('eyelink','eyelink');
% clear; load('eyelink'); scnsize = get(0,'ScreenSize');



srt_pro  =eyelink.srt(eyelink.anti==0 & eyelink.error==0 );
srt_anti =eyelink.srt(eyelink.anti==1 & eyelink.error==0 );
srt_pro_error =eyelink.srt(eyelink.anti==0 & eyelink.error==3 );
srt_anti_error=eyelink.srt(eyelink.anti==1 & eyelink.error==3 );
fig(202);set(gcf,'name','population trial by trial pro v anti SRT ');
subplot(2,2,1);ttest_bcoe(srt_anti,srt_pro); xlabel('srt-anti v srt-pro(SRT)');
subplot(2,2,2);ttest_bcoe(srt_anti_error,srt_pro_error);xlabel('srt-anti-error v srt-pro-error (SRT)');
subplot(2,2,3);mann_whitney(srt_pro,srt_pro_error);xlabel('srt-pro v srt-pro-error (SRT)');
subplot(2,2,4);mann_whitney(srt_anti,srt_anti_error);xlabel('srt-anti v srt-anti-error (SRT)');

r_srt_pro  =eyelink.rank_srt(eyelink.anti==0 & eyelink.error==0 );
r_srt_anti =eyelink.rank_srt(eyelink.anti==1 & eyelink.error==0 );
r_srt_pro_error =eyelink.rank_srt(eyelink.anti==0 & eyelink.error==3 );
r_srt_anti_error=eyelink.rank_srt(eyelink.anti==1 & eyelink.error==3 );
fig(203);set(gcf,'name','population trial by trial pro v anti ranked SRT ');
subplot(2,2,1);mann_whitney(r_srt_anti,r_srt_pro); xlabel('srr-anti v srr-pro (SRR)');
subplot(2,2,2);mann_whitney(r_srt_anti_error,r_srt_pro_error);xlabel('srr-anti-error v srr-pro-error (SRR)');
subplot(2,2,3);mann_whitney(r_srt_pro,r_srt_pro_error);xlabel('srr-pro v srr-pro-error (SRR)');
subplot(2,2,4);mann_whitney(r_srt_anti,r_srt_anti_error);xlabel('srr-anti v srr-anti-error (SRR)');


srt_pro_peri  =eyelink.srt(eyelink.anti ==0 & eyelink.error==0 & eyelink.peri==1);
srt_anti_peri =eyelink.srt(eyelink.anti ==1 & eyelink.error==0 & eyelink.peri==1);
srt_error_peri=eyelink.srt(eyelink.error==3 & eyelink.peri ==1);
srt_pro_cent  =eyelink.srt(eyelink.anti ==0 & eyelink.error==0 & eyelink.peri==0);
srt_anti_cent =eyelink.srt(eyelink.anti ==1 & eyelink.error==0 & eyelink.peri==0);
srt_error_cent=eyelink.srt(eyelink.error==3 & eyelink.peri ==0);
fig(204);clf,set(gcf,'name','population trial by trial PERI v CENT (pro/anti/error)')
subplot(2,3,1),ttest_bcoe(srt_pro_peri,srt_pro_cent);xlabel('srt-pro-peri v srt-pro-cent (SRT)');
subplot(2,3,2),ttest_bcoe(srt_anti_peri,srt_anti_cent);xlabel('srt-anti-peri v srt-anti-cent (SRT)');
subplot(2,3,3),ttest_bcoe(srt_error_peri,srt_error_cent);xlabel('srt-error-peri v srt-error-cent (SRT)');
subplot(2,3,4),mann_whitney(srt_pro_peri,srt_pro_cent);xlabel('srt-pro-peri v srt-pro-cent (SRT)');
subplot(2,3,5),mann_whitney(srt_anti_peri,srt_anti_cent);xlabel('srt-anti-peri v srt-anti-cent (SRT)');
subplot(2,3,6),mann_whitney(srt_error_peri,srt_error_cent);xlabel('srt-error-peri v srt-error-cent (SRT)');

r_srt_pro_peri  =eyelink.rank_srt(eyelink.anti ==0 & eyelink.error==0 & eyelink.peri==1);
r_srt_anti_peri =eyelink.rank_srt(eyelink.anti ==1 & eyelink.error==0 & eyelink.peri==1);
r_srt_error_peri=eyelink.rank_srt(eyelink.error==3 & eyelink.peri ==1);
r_srt_pro_cent  =eyelink.rank_srt(eyelink.anti ==0 & eyelink.error==0 & eyelink.peri==0);
r_srt_anti_cent =eyelink.rank_srt(eyelink.anti ==1 & eyelink.error==0 & eyelink.peri==0);
r_srt_error_cent=eyelink.rank_srt(eyelink.error==3 & eyelink.peri ==0);
fig(205);clf,set(gcf,'name','population trial by trial PERI v CENT rank (pro/anti/error)')
subplot(2,3,1),ttest_bcoe(r_srt_pro_peri,r_srt_pro_cent);xlabel('srr-pro-peri v srr-pro-cent (SRR)');
subplot(2,3,2),ttest_bcoe(r_srt_anti_peri,r_srt_anti_cent);xlabel('srr-anti-peri v srr-anti-cent (SRR)');
subplot(2,3,3),ttest_bcoe(r_srt_error_peri,r_srt_error_cent);xlabel('srr-error-peri v srr-error-cent (SRR)');
subplot(2,3,4),mann_whitney(r_srt_pro_peri,r_srt_pro_cent);xlabel('srr-pro-peri v srr-pro-cent (SRR)');
subplot(2,3,5),mann_whitney(r_srt_anti_peri,r_srt_anti_cent);xlabel('srr-anti-peri v srr-anti-cent (SRR)');
subplot(2,3,6),mann_whitney(r_srt_error_peri,r_srt_error_cent);xlabel('srr-error-peri v srr-error-cent (SRR)');



% 1-back crap SRT
fred=[0; (diff(eyelink.filenum))] % remember to remove comparisons between files
srt_pro_pro  =eyelink.srt([0; [fred(2:end)<1 & eyelink.anti(1:end-1)==0 & eyelink.anti(2:end)==0 & eyelink.error(2:end)==0]]>0 );
srt_anti_anti=eyelink.srt([0; [fred(2:end)<1 & eyelink.anti(1:end-1)==1 & eyelink.anti(2:end)==1 & eyelink.error(2:end)==0]]>0 );
srt_anti_pro =eyelink.srt([0; [fred(2:end)<1 & eyelink.anti(1:end-1)==1 & eyelink.anti(2:end)==0 & eyelink.error(2:end)==0]]>0 );
srt_pro_anti =eyelink.srt([0; [fred(2:end)<1 & eyelink.anti(1:end-1)==0 & eyelink.anti(2:end)==1 & eyelink.error(2:end)==0]]>0 );

fig(206),clf;set(gcf,'name','Eyelink2 - MEAN SRT Pro v Anti (1 back )')
subplot(2,1,1);hold on;axis([.5 2.5 550 650]);tickout; axis auto
ylabel('Saccadic Reaction Time')
xlim([.5 2.5]);
plot(2,mean(srt_pro_peri),'b');
plot(2,mean(srt_pro_cent),'b');
h=text(2,mean(srt_pro_peri),'peri');set(h,'color',[0 0 .2],'FontWeight','bold');
h=text(2,mean(srt_pro_cent),'cent');set(h,'color',[0.7 0.7 1],'FontWeight','bold');
hline(mean(srt_pro),[.5 .5 .8]);
h=text(2,mean(srt_pro),'pro');set(h,'color',[0 0 1],'FontWeight','bold');

plot(2,mean(srt_anti_peri),'r');
plot(2,mean(srt_anti_cent),'r');
h=text(2,mean(srt_anti_peri),'peri');set(h,'color',[.2 0 0],'FontWeight','bold');
h=text(2,mean(srt_anti_cent),'cent');set(h,'color',[1 0.7 0.7],'FontWeight','bold');
hline(mean(srt_anti),[.8 .5 .5]);
h=text(2,mean(srt_anti),'anti');set(h,'color',[ 1 0 0],'FontWeight','bold');

plot(1,mean(srt_pro_pro),'color',[0 0 1]);
plot(1,mean(srt_anti_pro),'color',[.5 0 1]);
plot(1,mean(srt_pro_anti),'color',[1 0 .5]);
plot(1,mean(srt_anti_anti),'color',[1 0 0]);
h=text(1,mean(srt_pro_pro),'pro->pro');set(h,'color',[0 0 1],'FontWeight','bold');
h=text(1,mean(srt_anti_pro),'anti->pro');set(h,'color',[.5 0 1],'FontWeight','bold');
h=text(1,mean(srt_pro_anti),'pro->anti');set(h,'color',[1 0 .5],'FontWeight','bold');
h=text(1,mean(srt_anti_anti),'anti->anti');set(h,'color',[1 0 0],'FontWeight','bold');

subplot(2,2,3);
ttest_bcoe(srt_anti,srt_pro);
subplot(2,2,4);
mann_whitney(srt_anti,srt_pro);

% 1-back crap SRR
fred=[0; (diff(eyelink.filenum))] % remember to remove comparisons between files
r_srt_pro_pro  =eyelink.rank_srt([0; [fred(2:end)<1 & eyelink.anti(1:end-1)==0 & eyelink.anti(2:end)==0 & eyelink.error(2:end)==0]]>0 );
r_srt_anti_anti=eyelink.rank_srt([0; [fred(2:end)<1 & eyelink.anti(1:end-1)==1 & eyelink.anti(2:end)==1 & eyelink.error(2:end)==0]]>0 );
r_srt_anti_pro =eyelink.rank_srt([0; [fred(2:end)<1 & eyelink.anti(1:end-1)==1 & eyelink.anti(2:end)==0 & eyelink.error(2:end)==0]]>0 );
r_srt_pro_anti =eyelink.rank_srt([0; [fred(2:end)<1 & eyelink.anti(1:end-1)==0 & eyelink.anti(2:end)==1 & eyelink.error(2:end)==0]]>0 );

fig(207);clf;set(gcf,'name','Eyelink2 - MEAN SRR Pro v Anti (1 back )')
subplot(2,1,1);hold on;axis([.5 2.5 550 650]);tickout; axis auto
ylabel('Saccadic Reaction RANK')
xlim([.5 2.5]); ylim([45 65])
plot(2,mean(r_srt_pro_peri),'b');
plot(2,mean(r_srt_pro_cent),'b');
h=text(2,mean(r_srt_pro_peri),'peri');set(h,'color',[0 0 .2],'FontWeight','bold');
h=text(2,mean(r_srt_pro_cent),'cent');set(h,'color',[0.7 0.7 1],'FontWeight','bold');
hline(mean(r_srt_pro),[.5 .5 .8]);
h=text(2,mean(r_srt_pro),'pro');set(h,'color',[0 0 1],'FontWeight','bold');

plot(2,mean(r_srt_anti_peri),'r');
plot(2,mean(r_srt_anti_cent),'r');
h=text(2,mean(r_srt_anti_peri),'peri');set(h,'color',[.2 0 0],'FontWeight','bold');
h=text(2,mean(r_srt_anti_cent),'cent');set(h,'color',[1 0.7 0.7],'FontWeight','bold');
hline(mean(r_srt_anti),[.8 .5 .5]);
h=text(2,mean(r_srt_anti),'anti');set(h,'color',[ 1 0 0],'FontWeight','bold');

plot(1,mean(r_srt_pro_pro),'color',[0 0 1]);
plot(1,mean(r_srt_anti_pro),'color',[.5 0 1]);
plot(1,mean(r_srt_pro_anti),'color',[1 0 .5]);
plot(1,mean(r_srt_anti_anti),'color',[1 0 0]);
h=text(1,mean(r_srt_pro_pro),'pro->pro');set(h,'color',[0 0 1],'FontWeight','bold');
h=text(1,mean(r_srt_anti_pro),'anti->pro');set(h,'color',[.5 0 1],'FontWeight','bold');
h=text(1,mean(r_srt_pro_anti),'pro->anti');set(h,'color',[1 0 .5],'FontWeight','bold');
h=text(1,mean(r_srt_anti_anti),'anti->anti');set(h,'color',[1 0 0],'FontWeight','bold');

subplot(2,2,3);
ttest_bcoe(r_srt_anti,r_srt_pro);
subplot(2,2,4);
mann_whitney(r_srt_anti,r_srt_pro);







fig(208);clf;hold on;ylim([200 1200]);
set(gcf,'Position',round(scnsize.*[1 1 1 .45]+[3 scnsize(4)/2-30 0 0]));
set(gcf,'Name','Subjects Saccadic Reaction Time');tickout;
Ylabel('Subjects Mean Saccadic Reaction Time');
a=[];b=[];c=[];
for i=1:max(eyelink.subjectnum)
    xlim([i-.5 i+.5]);
    vline(i);
    [cnt val]=hist_b(eyelink.srt(eyelink.subjectnum==i & eyelink.anti==0 & eyelink.error==0 ),25);
    cnt_a=cnt/40;
    hplot_v(val,-cnt_a+i,'vb',i);
    [cnt val]=hist_b(eyelink.srt(eyelink.subjectnum==i & eyelink.anti==1 & eyelink.error==0 ),25);
    cnt_a=cnt/40;
    hplot_v(val,cnt_a+i,'vr',i);
    a(end+1)=mean(eyelink.srt(eyelink.subjectnum==i & eyelink.anti==0 & eyelink.error==0 ));
    b(end+1)=mean(eyelink.srt(eyelink.subjectnum==i & eyelink.anti==1 & eyelink.error==0 ));
    c(end+1)=mean(eyelink.srt(eyelink.subjectnum==i & eyelink.error==3 ));
    hline(c(end));
    hline(b(end),'r');
    hline(a(end),'b');
end
plot(c,'color',[.7 .7 .7 ]);
plot(b,'r');
plot(a,'b');
subject_pro_srt=a;
subject_anti_srt=b;
subject_error_srt=c;
set(gca,'xtick',[1:max(eyelink.subjectnum)]);
set(gca,'xticklabel',eyelink.subject(:,1:2));
axis auto;ylim([200 1200]);

fig(210);clf,set(gcf,'Name','Population analysis by subject''s SRT & SRR');tickout;
subplot(1,2,1);
ttest_bcoe(b,a);
mann_whitney(b,a);
fig(209);clf;hold on;ylim([200 1200]);
set(gcf,'Position',round(scnsize.*[1 1 1 .45]+[3 scnsize(4)/2-30 0 0]));
set(gcf,'Name','Subjects Saccadic Reaction Rank');tickout;;
Ylabel('Subjects Mean Saccadic Reaction Rank')
a=[];b=[];c=[];
for i=1:max(eyelink.subjectnum)
    xlim([i-.5 i+.5]);
    vline(i);
    [cnt val]=hist_b(eyelink.rank_srt(eyelink.subjectnum==i & eyelink.anti==0 & eyelink.error==0 & eyelink.long==1),5);
    cnt_a=cnt/40;
    hplot_v(val,-cnt_a+i,'vb',i);
    [cnt val]=hist_b(eyelink.rank_srt(eyelink.subjectnum==i & eyelink.anti==1 & eyelink.error==0 & eyelink.long==1),5);
    cnt_a=cnt/40;
    hplot_v(val,cnt_a+i,'vr',i);
    a(end+1)=mean(eyelink.rank_srt(eyelink.subjectnum==i & eyelink.anti==0 & eyelink.error==0 ));
    b(end+1)=mean(eyelink.rank_srt(eyelink.subjectnum==i & eyelink.anti==1 & eyelink.error==0 ));
    c(end+1)=mean(eyelink.rank_srt(eyelink.subjectnum==i & eyelink.error==3 ));
    hline(c(end));
    hline(b(end),'r');
    hline(a(end),'b');
end
plot(c,'color',[.7 .7 .7 ]);
plot(b,'r');
plot(a,'b');
subject_pro_srr=a;
subject_anti_srr=b;
subject_error_srr=c;
set(gca,'xtick',[1:max(eyelink.subjectnum)]);
set(gca,'xticklabel',eyelink.subject(:,1:2));
axis auto;ylim([0 100]);
fig(210);
subplot(1,2,2);
%ttest_bcoe(b,a)
mann_whitney(b,a);

fig(211);clf;set(gcf,'name','Eyelink2 Subject''s SRTs (pro v anti)');
set(gcf,'Position',round(scnsize.*[1 1 1 .45]+[3 scnsize(4)/2-30 0 0]));
fig(212);clf;set(gcf,'name','Eyelink2 Subject''s SRRs (pro v anti)');
set(gcf,'Position',round(scnsize.*[1 1 1 .45]+[3 scnsize(4)/2-30 0 0]));
for i=1:max(eyelink.subjectnum)
    fig(211);
    subplot(2,max(eyelink.subjectnum)/2,i);
    ttest_bcoe(eyelink.srt(eyelink.subjectnum==i & eyelink.anti==1 & eyelink.error==0 ), eyelink.srt(eyelink.subjectnum==i & eyelink.anti==0 & eyelink.error==0 ));
    xlabel(eyelink.subject(i,1:2)); xlim([300 1000]); 
    fig(212);
    subplot(2,max(eyelink.subjectnum)/2,i);
    mann_whitney(eyelink.rank_srt(eyelink.subjectnum==i & eyelink.anti==1 & eyelink.error==0 ), eyelink.rank_srt(eyelink.subjectnum==i & eyelink.anti==0 & eyelink.error==0 ));
    xlabel(eyelink.subject(i,1:2));
end



fig(213);clf;hold on;ylim([200 1200]);
set(gcf,'Position',round(scnsize.*[1 1 1 .45]+[3 scnsize(4)/2-30 0 0]));
set(gcf,'Name','All Blocks');
Ylabel('Saccadic Reaction Time');
[a b]=hist_b(eyelink.filenum);
temp=b(a>1);

a=[];b=[];c=[];
for i = 1:length(eyelink.file_name);
    xlim([i-.5 i+.5]);
    vline(i);
    [cnt val]=hist_b(eyelink.srt(eyelink.filenum==temp(i) & eyelink.anti==0 & eyelink.error==0 ),25);
    cnt_a=cnt/10;
    hplot_v(val,-cnt_a+i,'vb',i);
    [cnt val]=hist_b(eyelink.srt(eyelink.filenum==temp(i) & eyelink.anti==1 & eyelink.error==0 ),25);
    cnt_a=cnt/10;
    hplot_v(val,cnt_a+i,'vr',i);
    a(end+1)=mean(eyelink.srt(eyelink.filenum==temp(i) & eyelink.anti==0 & eyelink.error==0));
    b(end+1)=mean(eyelink.srt(eyelink.filenum==temp(i) & eyelink.anti==1 & eyelink.error==0));
    c(end+1)=mean(eyelink.srt(eyelink.filenum==temp(i) & eyelink.error==3 ));
    hline(c(end));
    hline(b(end),'r');
    hline(a(end),'b');
    i=i+1;
end
plot(c,'color',[.7 .7 .7 ]);
plot(b,'r');
plot(a,'b');
block_pro_srt=a;
block_anti_srt=b;
block_error_srt=c;
set(gca,'xtick',1:length(eyelink.file_name));
set(gca,'xticklabel',eyelink.file_name);
set(gcf,'name','Eyelink2 Behavioral Data - - block by block');
axis tight;ylim([275 1025]);
fig(215),clf;
subplot(1,2,1);
ttest_bcoe(b,a);
xlabel('Mean Saccadic Reaction Time')

fig(214);clf;hold on;ylim([0 100]);
set(gcf,'Position',round(scnsize.*[1 1 1 .45]+[3 scnsize(4)/2-30 0 0]));
set(gcf,'Name','All Blocks');
Ylabel('normalized Saccadic Reaction Rank');
a=[];b=[];c=[];
for i = 1:length(eyelink.file_name);
    xlim([i-.5 i+.5]);
    vline(i);
    [cnt val]=hist_b(eyelink.rank_srt(eyelink.filenum==temp(i) & eyelink.anti==0 & eyelink.error==0 ),25);
    cnt_a=cnt/10;
    hplot_v(val,-cnt_a+i,'vb',i);
    [cnt val]=hist_b(eyelink.rank_srt(eyelink.filenum==temp(i) & eyelink.anti==1 & eyelink.error==0 ),25);
    cnt_a=cnt/10;
    hplot_v(val,cnt_a+i,'vr',i);
    a(end+1)=mean(eyelink.rank_srt(eyelink.filenum==temp(i) & eyelink.anti==0 & eyelink.error==0));
    b(end+1)=mean(eyelink.rank_srt(eyelink.filenum==temp(i) & eyelink.anti==1 & eyelink.error==0));
    c(end+1)=mean(eyelink.rank_srt(eyelink.filenum==temp(i) & eyelink.error==3 ));
    hline(c(end));
    hline(b(end),'r');
    hline(a(end),'b');
end
plot(c,'color',[.7 .7 .7 ]);
plot(b,'r');
plot(a,'b');
block_pro_srr=a;
block_anti_srr=b;
block_error_srr=c;
set(gca,'xtick',1:length(eyelink.file_name));
set(gca,'xticklabel',eyelink.file_name(:,2));
set(gcf,'name','Eyelink2 Behavioral Data - - block by block');
axis tight;ylim([0 100]);
fig(215);
subplot(1,2,2);
ttest_bcoe(b,a);
xlabel('Mean Saccadic Reaction Rank')




fig(216);clf;set(gcf,'name','PRO v ANTI Correct v Error Data (subject)');
set(gcf,'Position',round(scnsize.*[1 1 1 .45]+[3 30 0 0]));
tickout;
fred=[];
for i = 1:max(eyelink.subjectnum)
    pro_o(i) =sum(eyelink.subjectnum==i & eyelink.anti==0 & eyelink.error==0);
    pro_x(i) =sum(eyelink.subjectnum==i & eyelink.anti==0 & eyelink.error==3);
    anti_o(i)=sum(eyelink.subjectnum==i & eyelink.anti==1 & eyelink.error==0);
    anti_x(i)=sum(eyelink.subjectnum==i & eyelink.anti==1 & eyelink.error==3);
end   
fred=[fred; pro_o/(pro_o+pro_x) pro_x/(pro_o+pro_x) anti_o/(anti_o+anti_x) anti_x/(anti_o+anti_x)] ;

bar([fred; fred]);
xlim([.5 max(eyelink.subjectnum)+.5]);
set(gca,'xtick',1:max(eyelink.subjectnum));
set(gca,'xticklabel',eyelink.subject(:,2));
axis tight;ylim([0 100]);
Xlabel('Subject')
Ylabel('% Correct/Incorrect')


fig(217);clf;set(gcf,'name','PERI v CENT Correct v Error Data (subject)');
set(gcf,'Position',round(scnsize.*[1 1 1 .45]+[3 30 0 0]));
tickout;
fred=[];
for i = 1:max(eyelink.subjectnum)
    peri_pro_o =sum(eyelink.subjectnum==i & eyelink.peri==1 & eyelink.anti==0 & eyelink.error==0);
    peri_pro_x =sum(eyelink.subjectnum==i & eyelink.peri==1 & eyelink.anti==0 & eyelink.error==3);
    peri_anti_o=sum(eyelink.subjectnum==i & eyelink.peri==1 & eyelink.anti==1 & eyelink.error==0);
    peri_anti_x=sum(eyelink.subjectnum==i & eyelink.peri==1 & eyelink.anti==1 & eyelink.error==3);
    cent_pro_o =sum(eyelink.subjectnum==i & eyelink.peri==0 & eyelink.anti==0 & eyelink.error==0);
    cent_pro_x =sum(eyelink.subjectnum==i & eyelink.peri==0 & eyelink.anti==0 & eyelink.error==3);
    cent_anti_o=sum(eyelink.subjectnum==i & eyelink.peri==0 & eyelink.anti==1 & eyelink.error==0);
    cent_anti_x=sum(eyelink.subjectnum==i & eyelink.peri==0 & eyelink.anti==1 & eyelink.error==3);
    fred=[fred;...
            (peri_pro_o+peri_anti_o)/(peri_pro_o+peri_pro_x+peri_anti_o+peri_anti_x) ...
            (peri_pro_x+peri_anti_x)/(peri_pro_o+peri_pro_x+peri_anti_o+peri_anti_x) ...
            (cent_pro_o+cent_anti_o)/(cent_pro_o+cent_pro_x+cent_anti_o+cent_anti_x) ...
            (cent_pro_x+cent_anti_x)/(cent_pro_o+cent_pro_x+cent_anti_o+cent_anti_x)];
    
end
bar(fred);
xlim([.5 max(eyelink.subjectnum)+.5])
set(gca,'xtick',1:max(eyelink.subjectnum));
set(gca,'xticklabel',eyelink.subject(:,1:2));
Xlabel('Subject')
Ylabel('% Correct/Incorrect')




fig(218),clf;set(gcf,'name','Delay effect on SRT - long (culmulative)')
fig(219),clf;set(gcf,'name','Delay effect on SRT - long')
subplot(3,1,1);hold on;title('Delay effect on SRT (long trials)')
for i=0:.5:1
    subplot(3,1,1);tickout
    pro_fred=eyelink.srt(eyelink.anti==0 & eyelink.error==0 & eyelink.delay==i  & eyelink.long==1 );
    xlim([i-.25 i+.25]);
    hline(mean(pro_fred),'b');
%     h=text(ones(size(pro_fred))*i-.04, pro_fred,'p');
%     set(h,'color',[0 0 1],'FontWeight','bold')
    [cnt val]=hist_b(pro_fred,25);
    cnt_a=cnt/200;
    hplot_v(val,-cnt_a+i,'vb',i);
    fig(218);
    hplot(val,(cumsum(cnt)/sum(cnt)*.5)+i,'b');
    fig(219);

    anti_fred=eyelink.srt(eyelink.anti==1 & eyelink.error==0 & eyelink.delay==i & eyelink.long==1 );
    hline(mean(anti_fred),'r');
%     h=text(ones(size(anti_fred))*i+.02, anti_fred,'a');
%     set(h,'color',[1 0 0],'FontWeight','bold')
    [cnt val]=hist_b(anti_fred,25);
    cnt_a=cnt/200;
    hplot_v(val,cnt_a+i,'vr',i);
    axis([-.25 1.25 200 1400]);
    vline(i);
    fig(218);
    hplot(val,(cumsum(cnt)/sum(cnt)*.5)+i,'r');
    fig(219);
    
    subplot(3,3,(i/.5)+4);tickout;
    ttest_bcoe(anti_fred,pro_fred);
    subplot(3,3,(i/.5)+7);
    mann_whitney(anti_fred,pro_fred);
end
fig(218);





fig(220),clf;set(gcf,'name','Delay effect on SRR - long (culmulative)');
fig(221),clf;set(gcf,'name','Delay effect on SRR - long');
subplot(3,1,1);hold on;title('Delay effect on SRR (long trials)');
for i=0:.5:1
    subplot(3,1,1);tickout
    pro_fred=eyelink.rank_srt(eyelink.anti==0 & eyelink.error==0 & eyelink.delay==i  & eyelink.long==1 );
    xlim([i-.25 i+.25]);
    hline(mean(pro_fred),'b');
%     h=text(ones(size(pro_fred))*i-.04, pro_fred,'p');
%     set(h,'color',[0 0 1],'FontWeight','bold')
    [cnt val]=hist_b(pro_fred,5);
    cnt_a=cnt/200;
    hplot_v(val,-cnt_a+i,'vb',i);
    fig(220);
    hplot(val,(cumsum(cnt)/sum(cnt)*.5)+i,'b');
    fig(221);

    anti_fred=eyelink.rank_srt(eyelink.anti==1 & eyelink.error==0 & eyelink.delay==i & eyelink.long==1 );
    hline(mean(anti_fred),'r');
%     h=text(ones(size(anti_fred))*i+.02, anti_fred,'a');
%     set(h,'color',[1 0 0],'FontWeight','bold')
    [cnt val]=hist_b(anti_fred,5);
    cnt_a=cnt/200;
    hplot_v(val,cnt_a+i,'vr',i);
    axis([-.25 1.25 -5 105]);
    vline(i);
    fig(220);
    hplot(val,(cumsum(cnt)/sum(cnt)*.5)+i,'r');
    fig(221);
    
    subplot(3,3,(i/.5)+4);tickout;
    ttest_bcoe(anti_fred,pro_fred);
    subplot(3,3,(i/.5)+7);
    mann_whitney(anti_fred,pro_fred);
end
fig(220);


fig(222),clf;set(gcf,'name','Across Delay effect on SRR - long');
pro_0 =eyelink.rank_srt(eyelink.anti==0 & eyelink.error==0 & eyelink.delay==0.0 );
pro_5 =eyelink.rank_srt(eyelink.anti==0 & eyelink.error==0 & eyelink.delay==0.5 );
pro_1 =eyelink.rank_srt(eyelink.anti==0 & eyelink.error==0 & eyelink.delay==1.0 );
anti_0=eyelink.rank_srt(eyelink.anti==1 & eyelink.error==0 & eyelink.delay==0.0 );
anti_5=eyelink.rank_srt(eyelink.anti==1 & eyelink.error==0 & eyelink.delay==0.5 );
anti_1=eyelink.rank_srt(eyelink.anti==1 & eyelink.error==0 & eyelink.delay==1.0 );
subplot(2,3,1); mann_whitney(pro_5,pro_0);xlabel('SRR pro (0.5 v 0.0)')
subplot(2,3,2); mann_whitney(pro_1,pro_0);xlabel('SRR pro (1.0 v 0.0)')
subplot(2,3,3); mann_whitney(pro_1,pro_5);xlabel('SRR pro (0.5 v 1.0)')

subplot(2,3,4); mann_whitney(anti_5,anti_0);xlabel('SRR anti (0.5 v 0.0)')
subplot(2,3,5); mann_whitney(anti_1,anti_0);xlabel('SRR anti (1.0 v 0.0)')
subplot(2,3,6); mann_whitney(anti_1,anti_5);xlabel('SRR anti (0.5 v 1.0)')



disp('eyelink2_behavioral_anaysis02');
return
